require(['init'], function () {
    //获取当前月份
    $('#tableSt').css({
      "margin-top":"40px",

    })

    var nowdate = new Date();
    nowdate.setMonth(nowdate.getMonth()-1);
        var y = nowdate.getFullYear();
        var m = nowdate.getMonth()+1;
        var d = nowdate.getDate();
        var formatwdate = +'0'+y+'-'+'0'+m+'-'+d;
    var begintime=$("#beginTime").val(formatwdate);
    var endtime = new Date(new Date() - 1000 * 60*60*24).Format('YYYY-MM-DD');
    $('#endTime').attr('data-value', endtime)
    $('#endTime').val(endtime);
    agencyinformation();
    $('#Thequery1').on('click', function () {
       if ($('#box').html() == "") {
            agencyinformation(begintime);
        } else {
            $('#box').html("");
            agencyinformation(begintime);
        }
          $('#tableSt').hide();

    })

    function agencyinformation() {
        var begintime = $('#beginTime').val();
        var endtime = $('#endTime').val();
        ajaxMethod({
            url: '/api/coursetypes',
            data: {
                "begintime": begintime,
                "endtime":endtime
            },
            success: function (json) {
                var p = '<p style="margin:50px 44px">'+i18next.t('table.dataempty')+'</p>'//数据为空
                if (json == "") {
                    $('#box').html(p)
                } else {
                    formatData(json);
               }
             }
          })
        var $head = '<th style="border-bottom:1px solid #ccc;width:172px">'+i18next.t('table.teachers')+'</th>';
        var body = '';
        var $tbody=''
		    function  formatData(json){
          for(var i=0;i<json.length;i++){
            var coursetypename=json[i].coursetypename;
            var coursetypeuid=json[i].coursetypeuid;
            //console.log(data[i].coursetypename);
            $head +='<th data-id='+coursetypeuid+' style="border:1px solid #ccc;width:130px">'+coursetypename+'</th>';
          }
          //遍历出课程行
          $('#box').append('<p style="margin:10px 0;color:#398dee">'+i18next.t('table.company')+'：'+i18next.t('table.classhourr')+'</p><table id="table1"  class="table table-bordered table-hover" border="1" style="border-right: 1px solid #ccc;border-left: 1px solid #ccc;" ><thead><tr class="enroll" style="border:1px solid #ccc;border-bottom:none;width:130px">' + $head + '</tr></thead></table>')
          ajaxMethod({
            url: '/api/statistic/teacher',
            datatype: 'json',
            type: 'get',
            data: {
                "begintime": begintime,
                "endtime":endtime
            },
            success:function(data){
            classDats(data);
            }
          })
          function classDats(data){
            //遍历老师
            for(var i=0;i<data.length;i++){
              var str = '';
              var coursetypesN=data[i].coursetypes;
              var $tr = $('<tr class="enroll" style="border:1px solid #ccc;border-bottom:none;width:130px" data-uid='+data[i].teacheruid+' data-teachername='+data[i].teacher+'><td><a >'+data[i].teacher+'</a></td></tr>');
              //点击老师出现详细信息
              $tr.on('click',function(){
              var tid=$(this).attr('data-uid');
              // var begintime = $('#beginTime').val();
              // var endtime = $('#endTime').val();
              var teachername=$(this).attr("data-teachername");
              window.location.href="teacher_class.html?tid=" + tid + "&teachername="+teachername+"&begintime=" + begintime + "&endtime="+endtime;
              })
              //便利课程
      				 for(var k=0;k<json.length;k++){
                 var flag=false;
                 //遍历课程与时数
      					 for(var j=0;j<coursetypesN.length;j++){
      						if(!flag){
      							if(json[k].coursetypeuid === coursetypesN[j].id){
                    $tr.append('<td>'+coursetypesN[j].counts+'</td>');
      								flag=true;
                      //break;
      						 }
      					}
              }
               if(!flag){
                   $tr.append('<td></td>');
               }
             }
          $("#table1").append($tr);
        }


			}
    }
  }
})
